using System;
using System.Collections.Generic;
using System.Text;
using SmartMathLibrary;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides the finding of roots of linear splines.
    /// </summary>
    [Serializable]
    public class LinearSplineRootFinder
    {
        /// <summary>
        /// The linear spline for finding the roots.
        /// </summary>
        private LinearSpline spline;

        /// <summary>
        /// Initializes a new instance of the <see cref="LinearSplineRootFinder"/> class.
        /// </summary>
        /// <param name="spline">The linear spline for finding the roots.</param>
        public LinearSplineRootFinder(LinearSpline spline)
        {
            if (spline == (LinearSpline) null)
            {
                throw new ArgumentNullException("spline");
            }

            this.spline = spline;
        }

        /// <summary>
        /// Gets or sets the linear spline for finding the roots.
        /// </summary>
        /// <value>The linear spline for finding the roots.</value>
        public LinearSpline Spline
        {
            get { return spline; }
            set { spline = value; }
        }

        /// <summary>
        /// Finds all roots of the linear spline.
        /// </summary>
        /// <returns>The roots of the linear spline.</returns>
        public double[] FindRoots()
        {
            List<double> result = new List<double>();

            for (int i = 0; i < this.spline.Components.Count; i++)
            {
                double tempuri = this.spline.Components[i].LinearFunction.FindRoot();

                if ((this.spline.Components[i].Start.X < tempuri) && (tempuri < this.spline.Components[i].End.X))
                {
                    result.Add(tempuri);
                }
            }

            return result.ToArray();
        }
    }
}